#include "playscene.h"

#include <QDebug>
#include <QIcon>
#include <QPainter>
#include <QPixmap>
#include <QMenuBar>
#include <QMenu>
#include <QLabel>
#include <QFont>
#include <QMap>
#include <QVector>
#include <QSound>
#include <QPropertyAnimation> //动画类
#include "mypushbutton.h"
#include "dataconfig.h"
#include "mycoin.h"

PlayScene::PlayScene(int level)
{
    m_level = level;
    m_isWin = false;

    QString str = QString("level %1").arg(m_level);
    qDebug() << str;

    //设置固定大小
    setFixedSize(320, 583);

    //设置标题
    setWindowTitle("翻金币");

    //设置图标
    setWindowIcon(QIcon(":/title_icon.png"));

    QMenuBar* menubar = new QMenuBar(this);
    setMenuBar(menubar);

    QMenu* menu = menubar->addMenu("开始");
    menu->setParent(menubar);

    //创建菜单项
    QAction* action = menu->addAction("退出");
    action->setParent(menu);
    connect(action, &QAction::triggered, [=](){
        this->close();
    });

    QSound *backSound = new QSound(":/BackButtonSound.wav", this);
    QSound *clickCoinSound = new QSound(":/ConFlipSound.wav", this);
    QSound *levelWinSound = new QSound(":/LevelWinSound.wav", this);

    MyPushButton* backBtn = new MyPushButton(":/BackButton.png", ":/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width() - backBtn->width(), this->height() - backBtn->height());
    connect(backBtn, &QPushButton::clicked, [=](){
        qDebug() << "clicked back button";
        backSound->play();
        emit pressExitBtn();
    });

    QLabel* label = new QLabel;
    label->setParent(this);
    QString levelStr = QString("level %1").arg(m_level);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(25);
    label->setFont(font);
    label->setText(levelStr);
    label->setGeometry(QRect(30, this->height() - 50, 180, 50));

    //创建胜利图标
    QLabel* winLabel = new QLabel;
    winLabel->setParent(this);
    QPixmap winPix;
    winPix.load(":/LevelCompletedDialogBg.png");
    winLabel->setGeometry(QRect(0, 0, winPix.width(), winPix.height()));
    winLabel->setPixmap(winPix);
    winLabel->move((this->width() - winPix.width()) * 0.5, -winPix.height());

     //初始化游戏中的二维数组
    dataConfig dataCfg;
    QMap<int, QVector< QVector<int>>>::iterator it = dataCfg.mData.find(m_level);
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            m_coinData[i][j] = dataCfg.mData[this->m_level][i][j];
        }
    }


    //添加金币背景
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            QLabel* bg = new QLabel();
            bg->setParent(this);
            QPixmap pix;
            pix.load(":/BoardNode.png");
            bg->setGeometry(0, 0, pix.width(), pix.height());
            bg->setPixmap(pix);
            bg->move(60 + i * pix.width(), 200 + j * pix.height());

            Mycoin* coin = NULL;
            if(m_coinData[i][j] == 1)
            {
                coin = new Mycoin(":/Coin0001.png");
            }
            else
            {
                coin = new Mycoin(":/Coin0008.png");
            }

            coin->setParent(this);
            coin->move(62 + i * pix.width(), 204 + j * pix.height());
            coin->m_posx = i;
            coin->m_posy = j;
            coin->m_flag = m_coinData[i][j];
            m_coins[i][j] = coin;

            connect(coin, &QPushButton::clicked, [=](){
                clickCoinSound->play();
                coin->ChangeStatus();
                m_coinData[i][j] = m_coinData[i][j] == 0 ? 1 : 0;

                //延时翻动周围硬币
                QTimer::singleShot(200, this, [=]()
                {
                    //翻右侧硬币
                    if(coin->m_posx + 1 <= 3)
                    {
                        m_coins[coin->m_posx + 1][coin->m_posy]->ChangeStatus();
                        m_coinData[coin->m_posx + 1][coin->m_posy] = m_coinData[coin->m_posx + 1][coin->m_posy] == 0 ? 1 : 0;
                    }

                    //翻左侧硬币
                    if(coin->m_posx - 1 >= 0)
                    {
                        m_coins[coin->m_posx - 1][coin->m_posy]->ChangeStatus();
                        m_coinData[coin->m_posx - 1][coin->m_posy] = m_coinData[coin->m_posx - 1][coin->m_posy] == 0 ? 1 : 0;
                    }

                    //翻上方硬币
                    if(coin->m_posy - 1 >= 0)
                    {
                        m_coins[coin->m_posx][coin->m_posy - 1]->ChangeStatus();
                        m_coinData[coin->m_posx][coin->m_posy -1] = m_coinData[coin->m_posx][coin->m_posy - 1] == 0 ? 1 : 0;
                    }

                    //翻下方硬币
                    if(coin->m_posy + 1 <= 3)
                    {
                        m_coins[coin->m_posx][coin->m_posy + 1]->ChangeStatus();
                        m_coinData[coin->m_posx][coin->m_posy + 1] = m_coinData[coin->m_posx][coin->m_posy + 1] == 0 ? 1 : 0;
                    }

                    //在翻完周围金币后，判断是否都是金币，如果是，那么游戏胜利
                    m_isWin = true;
                    for(int i1 = 0; i1 < 4; i1++)
                    {
                        for(int j1 = 0; j1 < 4; j1++)
                        {
                            if(m_coins[i1][j1]->m_flag == 0)
                            {
                                m_isWin = false;
                                break;
                            }

                        }
                    }

                    if(m_isWin == true)
                    {
                        levelWinSound->play();
                        //将所有金币的胜利标志都置为true
                        for(int i1 = 0; i1 < 4; i1++)
                        {
                            for(int j1 = 0; j1 < 4; j1++)
                            {
                                m_coins[i1][j1]->m_isWin = true;
                            }
                        }

                        //将胜利的图片 显示出来
                        //创建动画对象. 参数1：设置动画的对象  参数2：动画的图形，这里是矩形
                        QPropertyAnimation* animation = new QPropertyAnimation(winLabel, "geometry");

                        //设置时间间隔 ms
                        animation->setDuration(1000);

                        //创建起始位置。 动画的图形是矩形，所以参数为矩形框
                        animation->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height()));

                        //创建结束位置
                        animation->setEndValue(QRect(winLabel->x(), winLabel->y() + 114, winLabel->width(), winLabel->height()));

                        //设置缓和曲线
                        animation->setEasingCurve(QEasingCurve::OutBounce);

                        //开始执行动画
                        animation->start();

                        qDebug() << "is win";

                        //delete animation;
                    }
                 });
            });
        }
    }

}


void PlayScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    QPixmap pix;
    pix.load(":/OtherSceneBg.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);

    pix.load(":/Title.png");
    painter.drawPixmap(20, 30, pix.width(), pix.height(), pix);
}
